﻿using System.ComponentModel;
using System.Reflection;

namespace SmartCity.Passport.API.Extensions
{
    public static class EnumExtensions
    {
        /// <summary>
        /// 获取枚举值的特性
        /// </summary>
        /// <param name="value">枚举的值</param>
        /// <returns>返回特性的值</returns>
        public static string GetDescription(this Enum value)
        {
            //获取枚举的反射信息
            FieldInfo field= value.GetType().GetField(value.ToString());
            //获取枚举的特性
            DescriptionAttribute attribute=field.GetCustomAttribute<DescriptionAttribute>();

            //获取特性的值
            return attribute==null ? value.ToString():attribute.Description;


        }
    }
}
